//阿拉伯数字转换为简写汉字
function Arabia_To_SimplifiedChinese(Num) {
  var i;
  for (i = Num.length - 1; i >= 0; i--) {
    Num = Num.replace(",", "") //替换Num中的“,”
    Num = Num.replace(" ", "") //替换Num中的空格
  }
  if (isNaN(Num)) { //验证输入的字符是否为数字
    //alert("请检查小写金额是否正确");
    return;
  }
  //字符处理完毕后开始转换，采用前后两部分分别转换
  var part = String(Num).split(".");
  var newchar = "";
  //小数点前进行转化
  for (i = part[0].length - 1; i >= 0; i--) {
    if (part[0].length > 10) {
      //alert("位数过大，无法计算");
      return "";
    } //若数量超过拾亿单位，提示
    var tmpnewchar = ""
    var perchar = part[0].charAt(i);
    switch (perchar) {
      case "0":
        tmpnewchar = "零" + tmpnewchar;
        break;
      case "1":
        tmpnewchar = "一" + tmpnewchar;
        break;
      case "2":
        tmpnewchar = "二" + tmpnewchar;
        break;
      case "3":
        tmpnewchar = "三" + tmpnewchar;
        break;
      case "4":
        tmpnewchar = "四" + tmpnewchar;
        break;
      case "5":
        tmpnewchar = "五" + tmpnewchar;
        break;
      case "6":
        tmpnewchar = "六" + tmpnewchar;
        break;
      case "7":
        tmpnewchar = "日" + tmpnewchar;
        break;
      case "8":
        tmpnewchar = "八" + tmpnewchar;
        break;
      case "9":
        tmpnewchar = "九" + tmpnewchar;
        break;
    }
    switch (part[0].length - i - 1) {
      case 0:
        tmpnewchar = tmpnewchar;
        break;
      case 1:
        if (perchar != 0) tmpnewchar = tmpnewchar + "十";
        break;
      case 2:
        if (perchar != 0) tmpnewchar = tmpnewchar + "百";
        break;
      case 3:
        if (perchar != 0) tmpnewchar = tmpnewchar + "千";
        break;
      case 4:
        tmpnewchar = tmpnewchar + "万";
        break;
      case 5:
        if (perchar != 0) tmpnewchar = tmpnewchar + "十";
        break;
      case 6:
        if (perchar != 0) tmpnewchar = tmpnewchar + "百";
        break;
      case 7:
        if (perchar != 0) tmpnewchar = tmpnewchar + "千";
        break;
      case 8:
        tmpnewchar = tmpnewchar + "亿";
        break;
      case 9:
        tmpnewchar = tmpnewchar + "十";
        break;
    }
    newchar = tmpnewchar + newchar;
  }
  //替换所有无用汉字，直到没有此类无用的数字为止
  while (newchar.search("零零") != -1 || newchar.search("零亿") != -1 || newchar.search("亿万") != -1 || newchar.search("零万") != -1) {
    newchar = newchar.replace("零亿", "亿");
    newchar = newchar.replace("亿万", "亿");
    newchar = newchar.replace("零万", "万");
    newchar = newchar.replace("零零", "零");
  }
  //替换以“一十”开头的，为“十”
  if (newchar.indexOf("一十") == 0) {
    newchar = newchar.substr(1);
  }
  //替换以“零”结尾的，为“”
  if (newchar.lastIndexOf("零") == newchar.length - 1) {
    newchar = newchar.substr(0, newchar.length - 1);
  }
  return newchar;
}

//求数组中连续最长的数
function maxSequence(array, step) {
  var _array = array.slice(), //clone array
    _step = 1,
    _arrayTemp = [],
    i = 0;

  var parseLogic = {
    //result container
    parseResults: [],
    //set value to array,what's the last array of parseResults
    set: function (n) {
      this.parseResults[this.parseResults.length - 1].push(n);
    },
    //get the last array from parseResults
    get: function () {
      return this.parseResults[this.parseResults.length - 1];
    },
    //put a new array in parseResults
    addItem: function () {
      this.parseResults.push([]);
    },
    //sort parseResults
    sortByAsc: function () {
      this.parseResults.sort(function (a, b) {
        return a.length - b.length;
      });
    }
  };

  //check params
  _step = step || _step;

  //sort array by asc
  _array.sort(function (a, b) {
    return a - b;
  });

  //remove repeat of data
  for (i = 0; i < _array.length; i++) {
    if (_array[i] != _array[i + 1]) {
      _arrayTemp.push(_array[i]);
    }
  }
  _array = _arrayTemp.slice();
  _arrayTemp = [];

  //parse array
  parseLogic.addItem();
  for (i = 0; i < _array.length; i++) {
    if (_array[i] + _step == _array[i + 1]) {
      parseLogic.set(_array[i]);
      continue;
    }
    if (_array[i] - _step == _array[i - 1]) {
      parseLogic.set(_array[i]);
      parseLogic.addItem();
    }
  }

  //sort result
  parseLogic.sortByAsc();

  //get the max sequence
  return parseLogic.get();

}

//数组中加入数组索引方法
Array.prototype.indexOf = function (val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
  }
  return -1;
};

//数组中加入移除方法
Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

//转换日期格式
function reloadMonth(val, continuous) {
  var loadData = "";
  if (continuous) {
    return "周" + Arabia_To_SimplifiedChinese(val[0]) + "至周" + Arabia_To_SimplifiedChinese(val[val.length - 1])
  } else {
    loadData = val.map((item, index) => {
      return "周" + Arabia_To_SimplifiedChinese(item);
    })
  }
  return loadData;
}

//根据数字排序显示日期
function maxMonthSequence(arr) {
  arr = arr.map((toNItem)=>{return Number(toNItem)});
  var arrList = arr;
  var maxSequenceData = maxSequence(arrList);
  //去除选中的
  if (maxSequenceData.length) {
    maxSequenceData.forEach((item, index) => {
      arrList.remove(item);
    })
    if (arrList.length) {
      if (maxSequenceData[0] > arrList[0]) {
        return reloadMonth(arrList).join(" ") + " " + reloadMonth(maxSequenceData, 1);
      } else {
        return reloadMonth(maxSequenceData, 1) + " " + reloadMonth(arrList).join(" ");
      }
    } else {
      return reloadMonth(maxSequenceData, 1);
    }
  } else {
    return reloadMonth(arrList).join(" ");
  }
}

module.exports = {
  maxMonthSequence: maxMonthSequence
}